
import javax.microedition.lcdui.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author sherin
 */
public class FileBrowser{
    String[] Response;
    private List fileBrowserMenu = null;
    TextBox fileContents;
    String copyingFile = null;
    private String pwd = "/home/major"; // TODO, home is not decided by the client. so must shift this to the server side
    String homeDirectory = "fileBrowser\nls\n"+pwd;
    public static Command deleteFile = new Command("Delete", Command.OK,5);
    public static Command copyFile = new Command("Copy", Command.OK, 6);
    public static Command cutFile = new Command("Cut", Command.OK, 6);
    public static Command pasteFile = new Command("Paste", Command.OK, 6);
    private Command closeFile = new Command("Close", Command.OK,6);
    private Command saveFile = new Command("Save", Command.OK, 6);
 //  private String pwd = "D:"; //for windows
   
    public FileBrowser(){
        //Response = ResponseArray;
        
        
    }

    public List displayFileBrowser( String[] Response ){
        fileBrowserMenu = new List(Response[2], Choice.IMPLICIT);
        //TODO  it should read from the mobile memory
        //index 1 contains the pwd
        for( int i = 3; i< Response.length;i++)
        {
            fileBrowserMenu.append(Response[i], null);
        }

   //     fileBrowserMenu.addCommand(Client.exitCommand);
        fileBrowserMenu.addCommand(lib.mainMenuCommand);
        fileBrowserMenu.addCommand(lib.backCommand);
        fileBrowserMenu.addCommand(deleteFile);
        fileBrowserMenu.addCommand(cutFile);
        fileBrowserMenu.addCommand(copyFile);
        fileBrowserMenu.addCommand(pasteFile);
//        fileBrowserMenu.setCommandListener( this );
        Client.currentMenu = "Browser Menu";
    //    Client.display.setCurrent(fileBrowserMenu);
        System.out.println( " In the file browser menu ******");
        return( fileBrowserMenu );
      //  
    }

    public String fileDelete(String dir) {
        StringBuffer message = new StringBuffer();
        message.append("fileBrowser\ndelete\n");
        System.out.println(dir+"   =========");  // pwd is pwd itself becaues once you delete a file the pwd has to be intact
        message.append(pwd);
        message.append("\n");
        message.append(dir);
        System.out.println(".............."+message);
        return message.toString();
    }

    public String requestFile(String dir) {
        StringBuffer message = new StringBuffer();
        message.append("fileBrowser\nls\n");
        System.out.println(dir+"   =========");
        pwd = pwd+"/"+ dir;
        message.append(pwd);
        message.append("\n");
        return message.toString();
    }

    String closeFile() {
        //TODO the pwd inconsistency when it is a file or folder
        StringBuffer message = new StringBuffer();
        message.append("fileBrowser\nls\n");
        removeFileNameFromPwd(); //in order to prevent the inconsistency because the client does not whether its a file or folder
        message.append(pwd);
        System.out.println( "this is the pwd ------   " + pwd );
        message.append("\n");
        return message.toString();
    }

    String saveFile() {
        StringBuffer message = new StringBuffer();
        message.append("fileBrowser\nsave\n");
        message.append(pwd); //which is infact is the file itself
        message.append("\n");
        String changedContent = fileContents.getString();
        System.out.println( "....." + changedContent);
        message.append(changedContent);
        message.append("\n");
        return message.toString();

    }

    TextBox displayFile(String[] ResponseArray) {
        fileContents = new TextBox(ResponseArray[2], ResponseArray[4],1024, TextField.ANY );
        fileContents.addCommand(closeFile);
        fileContents.addCommand(saveFile);
        return fileContents;
    }

    void fileCopy(String dir) {
        copyingFile = pwd+"/"+dir; //TODO
    }

    String pasteFile(String dir){
        StringBuffer message = new StringBuffer();
        message.append("fileBrowser\npaste\n");
        message.append(pwd);
        message.append("\n");
        message.append(copyingFile);
        return message.toString();
    }

    String previousFolder() {
        removeFileNameFromPwd();
        if( pwd.length() == 0)
            pwd = "/";
        StringBuffer message = new StringBuffer();
        message.append("fileBrowser\nls\n");
        message.append(pwd);
        message.append("\n");
        return message.toString();
    }

    void removeFileNameFromPwd(){
        int indexEnd = pwd.lastIndexOf('/');
        pwd = pwd.substring(0, indexEnd);
    }


    
}
